import logging
from datetime import datetime

from airflow.models import User
from airflow.models.idata_auth_resource import AuthResource
from airflow.models.idata_auth_role import AuthRole
from airflow.models.idata_auth_user_role import AuthUserRole
from airflow.models.idata_project import Project
from airflow.utils.session import create_session, provide_session

log = logging.getLogger(__name__)


@provide_session
def get_user(uid, session: None):
    user = User.get_user(uid, session)
    oa_user_name = user.oa_user_name
    roles = AuthRole.get_role(None, oa_user_name, session)
    for role in roles:
        rid = role.id
        resources = AuthResource.get_resource(None, rid, session)
        role.resources = resources
    user.roles = roles
    return user


@provide_session
def is_admin(oa_user_name, session: None):
    roles = AuthRole.get_role(None, oa_user_name, session)
    for role in roles:
        rid = role.id
        if rid == 1 or role.role_name == "管理员":
            return True
    return False


@provide_session
def add_user(user_name, oa_user_name, department_name, create_by, role_ids, session: None):
    user = User(user_name, oa_user_name, department_name, create_by, datetime.now())
    User.add_user(user, session)
    for role_id in role_ids:
        user_role = AuthUserRole(role_id, oa_user_name, datetime.now())
        AuthUserRole.add_user_role(user_role, session)


def delete_user(id, oa_user_name):
    with create_session() as session:
        User.delete_user(id, session)
        AuthUserRole.delete_user_role_by_oausername(oa_user_name, session)


def update_user(id, user_name, oa_user_name, department_name, create_by, role_ids):
    with create_session() as session:
        user = User(user_name, oa_user_name, department_name, create_by, datetime.now())
        user.id = id
        User.update_user(user, session)
        AuthUserRole.delete_user_role_by_oausername(oa_user_name, session)
        for role_id in role_ids:
            user_role = AuthUserRole(role_id, oa_user_name, datetime.now())
            AuthUserRole.add_user_role(user_role, session)


@provide_session
def get_users(page, session=None):
    if page == 0:
        users = User.get_users(session)
        for user in users:
            oa_user_name = user.oa_user_name
            roles = AuthRole.get_role(None, oa_user_name, session)
            for role in roles:
                rid = role.id
                resources = AuthResource.get_resource(None, rid, session)
                role.resources = resources
            user.roles = roles
        map_user = map(User.to_json, users)
        return list(map_user)
    else:
        user_result = User.get_users_page(page, session)
        for user in user_result.items:
            oa_user_name = user.oa_user_name
            roles = AuthRole.get_role(None, oa_user_name, session)
            for role in roles:
                rid = role.id
                resources = AuthResource.get_resource(None, rid, session)
                role.resources = resources
            user.roles = roles
        user_result.items = list(map(User.to_json, user_result.items))
        result = user_result.to_json()
        return result


@provide_session
def get_user_projects(page, oa_username, session=None):
    if page == 0:
        projects = Project.get_projects_by_oa_user_name(oa_username, session)
        map_projects = map(Project.to_json, projects)
        return list(map_projects)
    else:
        projects_result = Project.get_projects_by_oa_user_name_page(oa_username, page, session)
        projects_result.items = list(map(Project.to_json, projects_result.items))
        result = projects_result.to_json()
        return result
